package com.zhy.generator.codegen.mybatis.javarequest.elements;

import com.zhy.generator.codegen.api.IntrospectedColumn;
import com.zhy.generator.codegen.dom.java.Field;
import com.zhy.generator.codegen.dom.java.InnerClass;
import com.zhy.generator.codegen.dom.java.JavaVisibility;
import com.zhy.generator.codegen.dom.java.TopLevelClass;
import com.zhy.generator.codegen.mybatis.RootClassInfo;
import com.zhy.generator.codegen.mybatis.javarequest.RequestInnerClassConstant;

import java.util.List;

import static com.zhy.generator.codegen.util.JavaBeansUtil.getJavaBeansField;

/**
 * @author 源
 */
public class EditParamInnerClassGenerator extends AbstractJavaRequestInnerClassGenerator {
    @Override
    public void addInterfaceElements(TopLevelClass tlc) {
        InnerClass ic = new InnerClass(RequestInnerClassConstant.EDIT_PARAM);
        ic.setVisibility(JavaVisibility.PUBLIC);
        ic.setStatic(true);
        tlc.addInnerClass(ic);

        //添加实现类
        ic.addSuperInterface(getSuperInterface());

        //serialVersionUID
        addSerialVersionUIDField(ic);

        //Data注解
        addDataAnnotation(tlc,ic);

        //APiModel注解
        addApiModelAnnotation(tlc,ic);

        List<IntrospectedColumn> introspectedColumns = introspectedTable.getAllColumns();

        List<IntrospectedColumn> primaryKeyColumns = introspectedTable.getPrimaryKeyColumns();

        String rootClass = getSuperClass().getFullyQualifiedName();
        RootClassInfo instance = RootClassInfo.getInstance(rootClass, warnings);
        for (IntrospectedColumn introspectedColumn : introspectedColumns) {

            if (instance.containsProperty(introspectedColumn)) {
                continue;
            }

            Field field = getJavaBeansField(introspectedColumn, context, introspectedTable);

            if(primaryKeyColumns.contains(introspectedColumn)){
                addFieldAnnotation(tlc,field,introspectedColumn.getFieldComment(),true);
            }else{
                addFieldAnnotation(tlc,field,introspectedColumn.getFieldComment(),false);
            }

            ic.addField(field);
        }
    }
}
